package com.zhongnan.demo6.shengchanxiaofei;

public class Cooker extends Thread {
    private Desk t;

    public Cooker(Desk t) {
        this.t = t;
    }
    /*厨师步骤
    判断桌子上是否有汉堡，有就等待
    没有就生产
    把汉堡放在桌上
    叫醒等待的消费者
     */
    @Override
    public void run() {

        while (true) {
            synchronized (t.getLock()) {

                if (t.getCount() == 0) {
                    break;
                } else {
                    if (!t.isFlag()) {
                        System.out.println("厨师生产了汉堡");
                        t.setFlag(true);
                        t.getLock().notifyAll();
                        t.setCount(t.getCount() - 1);
                    } else {
                        try {
                            t.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

